package com.niiwoo.civet.trade.service.local.newRefund.notify.dubbo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.asset.cardniu.dto.request.NotifyRepayResultReqDTO;
import com.niiwoo.asset.cardniu.service.refund.RefundDubboService;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.base.enums.RefundTypeEnum;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.RefundNotifyRecord;
import com.niiwoo.civet.trade.dto.response.cardniu.RepayResultDTO;
import com.niiwoo.civet.trade.service.cardniu.CardNiuLoanDubboService;
import com.niiwoo.civet.trade.service.local.cardniu.CardNiuRefundLocalService;
import com.niiwoo.civet.trade.service.local.newRefund.notify.RefundNotifyHandle;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyDTO;
import com.niiwoo.civet.trade.service.local.newRefund.notify.dto.RefundNotifyHandleResult;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyEnum;
import com.niiwoo.civet.trade.service.local.newRefund.notify.enums.RefundNotifyRefundSourceEnum;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CardNiuRefundResultNotify implements RefundNotifyHandle {

	@Autowired
	private CardNiuRefundLocalService cardNiuRefundLocalService;
	
	@Reference(version = "1.0.0")
	private RefundDubboService refundDubboService;
	
	@Reference(version = "1.0.0")
	private BankCardDubboService bankCardDubboService;
	
	@Reference(version = "1.0.0")
	private CardNiuLoanDubboService cardNiuLoanDubboService;

	@Override
	public RefundNotifyHandleResult handle(RefundNotifyDTO refundNotifyDTO) {
		RefundNotifyHandleResult refundNotifyHandleResult = null;
		try {
			NotifyRepayResultReqDTO notifyRepayResultReqDTO = (NotifyRepayResultReqDTO) bulidParam(refundNotifyDTO);
			log.info("卡牛-还款结果通知 ，ProjectId：{}， 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(notifyRepayResultReqDTO));
			refundDubboService.notifyRepayResult(notifyRepayResultReqDTO);
			log.info("卡牛-还款结果通知完成 ，ProjectId：{}， 请求参数：{}", refundNotifyDTO.getProject().getProjectId(), JSONObject.toJSONString(notifyRepayResultReqDTO));
			refundNotifyHandleResult = new RefundNotifyHandleResult();
		} catch (Exception e) {
			refundNotifyHandleResult = new RefundNotifyHandleResult(e.getMessage());
			log.error("卡牛-还款结果通知异常，异常原因：{},请求参数:{}", e.getMessage(), JSONObject.toJSONString(refundNotifyDTO.getRefundNotifyRecord()));
		}
		return refundNotifyHandleResult;
	}	
	
	@Override
	public Object bulidParam(RefundNotifyDTO refundNotifyDTO) {
		
		Project project = refundNotifyDTO.getProject();
		RefundNotifyRecord refundNotifyRecord = refundNotifyDTO.getRefundNotifyRecord();
		
		NotifyRepayResultReqDTO notifyRepayResultReqDTO = new NotifyRepayResultReqDTO();
		// 查询卡牛还款结果
		RepayResultDTO repayResultDTO = cardNiuRefundLocalService.queryRepayResult(project.getProjectId(),
				refundNotifyRecord.getOrgRequestNo(), refundNotifyRecord.getRefundOrderId());
		notifyRepayResultReqDTO.setProjectId(project.getProjectId());
		notifyRepayResultReqDTO.setOrgRequestNo(refundNotifyRecord.getOrgRequestNo());
		notifyRepayResultReqDTO.setFailMessage(refundNotifyRecord.getRefundMessage());
		BeanUtils.copyProperties(repayResultDTO, notifyRepayResultReqDTO);
		// 请求来源：1-卡牛主动还款，2-你我金融app还款，3-系统代扣
		if (RefundNotifyRefundSourceEnum.APP.getCode().equals(refundNotifyRecord.getRefundSource())) {
			notifyRepayResultReqDTO.setRequestType(2);
		} else if (RefundNotifyRefundSourceEnum.THIRD_ORG.getCode().equals(refundNotifyRecord.getRefundSource())) {
			notifyRepayResultReqDTO.setRequestType(1);
		} else {
			notifyRepayResultReqDTO.setRequestType(3);
		}
		// 还款成功根据还款订单判断，还款失败默认正常还款
		if (null != refundNotifyDTO.getRefundOrder()) {
			if ((RefundTypeEnum.OVERDEPT.getRefundType()).equals(refundNotifyDTO.getRefundOrder().getRefundType())) {
				notifyRepayResultReqDTO.setRepayType(2);
			} else {
				notifyRepayResultReqDTO.setRepayType(1);
			}
		} else {
			notifyRepayResultReqDTO.setRepayType(1);
		}
		return notifyRepayResultReqDTO;
	}
	
	@Override
	public RefundNotifyEnum getType() {
		return RefundNotifyEnum.CARDNIU_REFUND_RESULT;
	}
}